//#define TSFUNCS_EXCLUDE_MINHOOK /* This program doesn't use any hooks */

#include "MinHook/MinHook.h"

#include "TSfuncs/TSFuncs.cpp"
#include "TSfuncs/TSHooks.cpp"
#include "TSfuncs/TSVector.cpp"

#include "BLstructs/include.hpp"

struct
{
	float start_x, start_y, start_z = 0;
	float end_x, end_y, end_z = 0;
	
} lastRaycast;


//bool castRay(const Point3F &start, const Point3F &end, U32 mask, RayInfo* info);
BlFunctionDef(bool, __thiscall, Container__castRay, void*,void*,void*,void*,void*);
BlFunctionHookDef(Container__castRay);
bool __fastcall Container__castRayHook( void* thisObj, void* blank, Point3F* start, Point3F* end, void* mask, void* info )
{
	lastRaycast.start_x = start->pX;
	lastRaycast.start_y = start->pY;
	lastRaycast.start_z = start->pZ;
	
	lastRaycast.end_x = end->pX;
	lastRaycast.end_y = end->pY;
	lastRaycast.end_z = end->pZ;
	
	return Container__castRayOriginal( thisObj, start, end, mask, info );
}


bool init()
{
	BlInit;

	if (!tsf_InitInternal())
		return false;

	//0x59ef40	Container__castRay
	BlScanFunctionHex(Container__castRay, "55 8b ec 83 e4 f8 81 ec 9c 01 00 00 ff 05 ?? ?? ?? ??");
	BlCreateHook(Container__castRay);
	BlTestEnableHook(Container__castRay);

	BlPrintf("    lastRaycast pointer: 0x%p", &lastRaycast);
	
	BlPrintf("%s (v%s-%s): init'd", PROJECT_NAME, PROJECT_VERSION, TSFUNCS_DEBUG ? "debug":"release");

	return true;
}

bool deinit(bool terminating)
{
	printf("%s: deinit'd\n", PROJECT_NAME);
	if(!terminating)
		BlTestDisableHook(Container__castRay);

	return true;
}

bool __stdcall DllMain(HINSTANCE hinstance, int reason, void *reserved)
{
	switch (reason)
	{
		case DLL_PROCESS_ATTACH:
			return init();
		case DLL_PROCESS_DETACH:
			return deinit(reserved != NULL);
		default:
			return true;
	}
}

extern "C" void __declspec(dllexport) __cdecl PROJECT_EXPORT(){}
